package com.heima.wemedia.controller.v1;

import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/v1/channel")
public class WmchannelController {


    @Autowired
    private WmChannelService wmChannelService;

    /**
     * 查询频道列表
     *
     * @return
     */
    @GetMapping("/channels")
    public ResponseResult findAll() {
        return wmChannelService.findAll();
    }


    /**
     * 查询频道列表
     *
     * @param channelDto 查询条件
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody ChannelDto channelDto) {
        PageResponseResult pageResponseResult = wmChannelService.listByConditin(channelDto);
        return pageResponseResult;
    }

    /**
     * 根据id删除频道
     * @param id id
     * @return
     */
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        wmChannelService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存
     * @param wmChannel
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmChannel wmChannel) {
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault((short)0);
        wmChannelService.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存
     * @param wmChannel
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmChannel wmChannel) {
        wmChannelService.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}